import urllib.request
import urllib.error
import re
import random
class uaip():
    def __init__(self):
        self.request = urllib.request
        self.uas = [("User-Agent",
                "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4549.400 QQBrowser/9.7.12900.400")
            ,
               ("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 OPR/26.0.1656.60Opera/8.0 (Windows NT 5.1; U; en)Mozilla/5.0 (Windows NT 5.1; U; en; rv:1.8.1) Gecko/20061208 Firefox/2.0.0 Opera 9.50Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.50"),
               ("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2")
               ]
        self.ipPools = []

    def getData(self,url,decodeMethod):
        self.request.build_opener().addheaders = [random.choice(self.uas)]
        self.getIP()
        try:
            data = self.request.urlopen(url).read().decode(decodeMethod,"ignore")
            return  data
        except urllib.error.URLError as error:
            if (hasattr(error, "code")):
                print("错误码" + str(error.code))
            if (hasattr(error, "reason")):
                print("错误原因：" + error.reason)



    def getIP(self):
        if(len(self.ipPools) == 0):
           url = "http://tvp.daxiangdaili.com/ip/?tid=556457474907029&filter=on&num=5&sortby=time&foreign=only&category=2"
           try:
               data = self.request.urlopen(url).read().decode("utf-8","ignore")
               ipList = data.split("\r\n")
               print(ipList)
               self.ipPools=[]
               for ip in ipList:
                   self.ipPools.append(ip)
           except urllib.error.URLError as error:
               if(hasattr(error,"code")):
                   print("错误码" + str(error.code))
               if(hasattr(error,"reason")):
                    print("错误原因：" + error.reason)

        proxy = self.request.ProxyHandler({"http": self.ipPools[0]})
        self.request.build_opener(proxy, self.request.ProxyHandler)
        del self.ipPools[0]

class JD():
    def __init__(self):
        self.uaip = uaip()
        pass
    def getData(self):
        infoFile = open("C:\\Users\\admin\\Desktop\\JD.txt","a+")
        pat = '<div class="p-price">.*?<div class="p-name p-name-type-2">.*?<div class="p-shop".*?</div>'
        pricePat = '<em>￥</em><i>(.*?)</i></strong>'
        namePat = '<div class="p-name p-name-type-2">.*?title="(.*?)"'
        commentNumPat = '<div class="p-commit">.*?">(.*?)</a>条评价'
        shopNamePat = '<div class="p-shop".*?title="(.*?)"'
        pidPat = '<a id="J_comment_(\d+)"'
        data = self.uaip.getData("http://search.jd.com/Search?keyword=%E9%9B%B6%E9%A3%9F&enc=utf-8&wq;=%E9%9B%B6%E9%A3%9F&pvid=ss7fo4wi.ri9kq2","utf-8")
        result = re.compile(pat,re.S).findall(data)
        i = 1
        for info in result:
            price = re.compile(pricePat, re.S).findall(data)
            name = re.compile(namePat, re.S).findall(data)
            commentNum = re.compile(commentNumPat, re.S).findall(data)
            shopName = re.compile(shopNamePat, re.S).findall(data)
            pid = re.compile(pidPat, re.S).findall(data)
            name = "京东自营"
            if(len(shopName) != 0):
                name = shopName[0]
            infoFile.write('商品名：'+ name[0] + ",价格：" + price[0] + ",供应商：" + name + ",评论数：" + commentNum[0] + "\n")
            print('商品名：'+ name[0] + ",价格：" + price[0] + ",供应商：" + name + ",评论数：" + commentNum[0])
            self.getComment(pid[0],0,i)
            i = i+1
        infoFile.close()
    def getComment(self,pid,num,index):
        commentFile = open("C:\\Users\\admin\\Desktop\\comment\\" + str(index) + ".txt","a+")
        url = 'https://sclub.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98vv34346&productId='+ pid + '&score=0&sortType=5&page='+str(num)+'&pageSize=10&isShadowSku=0&rid=0&fold=1'
        commentData = self.uaip.getData(url,"GB2312")
        contentPat = '"id".*?"topped".*?"content":"(.*?)"'
        result = re.compile(contentPat,re.S).findall(commentData)
        for comment in result:
            commentFile.write(comment + "\n")
        commentFile.close()
        if(len(result) == 10):
            num = num + 1
            self.getComment(pid,num,index)


jd = JD()
jd.getData()